package com.share.device.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.share.common.core.web.domain.AjaxResult;
import com.share.device.domain.*;
import com.share.device.mapper.TicketsMapper;
import com.share.device.service.*;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.share.common.core.web.domain.AjaxResult.error;
import static com.share.common.core.web.domain.AjaxResult.success;

@Service
public class TicketsServiceImpl extends ServiceImpl<TicketsMapper, Tickets> implements TicketsService {
    @Autowired
    private TicketsMapper ticketsMapper;
    @Autowired
    private CabinetService cabinetService;
    @Autowired
    private StationService stationService;
    @Autowired
    private PersonnelService personnelService;
    @Autowired
    private AreaService areaService;
    @Override
    public List<Tickets> selectTicketsList(Tickets tickets) {
        List<Tickets> ticketsList = ticketsMapper.selectTicketsList(tickets);
        return ticketsList;
    }

    @Override
    public AjaxResult getAreaOps(String cabinetNo) {
        // 获取机柜信息
        Cabinet cabinet = cabinetService.getOne(new QueryWrapper<Cabinet>().eq("cabinet_no", cabinetNo));
        if(cabinet == null){
            return error("没有该柜机");
        }
        //获取该柜机所在的区域id
        //获取站点信息
        Station station = stationService.getOne(new QueryWrapper<Station>().eq("id", cabinet.getStationId()));
        if(station == null){
            return error("无法确定当前所在站点");
        }
        //根据站点id获取区域信息
        Area area = areaService.getOne(new QueryWrapper<Area>().eq("id", station.getAreaId()));
        if(area == null){
            return error("无法确定当前所在区域");
        }
        //根据区域id获取区域运维人员
        Personnel personnel = personnelService.getOne(new QueryWrapper<Personnel>()
                .eq("area_id", area.getId())
                .eq("role_id", 2)
                .eq("state", 0));
        //判断区域运维人员是否存在
        if(personnel == null){
            return error("区域运维人员不存在");
        }
        return success("获取成功", personnel.getId());
    }

    @Override
    public Tickets getDetailTickets(Long id) {
        return ticketsMapper.getDetailTickets(id);
    }
}
